package com.kzd.sdk.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kzd.sdk.base.OwnerActivity;
import com.kzd.sdk.callback.KZD_HttpCallback;
import com.kzd.sdk.core.KZD_ApiManager;
import com.kzd.sdk.core.KZD_SdkController;
import com.kzd.sdk.entity.KZD_UserInfo;
import com.kzd.sdk.utils.AppUtils;

public class KZD_UserVerifyActivity extends OwnerActivity {

    public static void open(Context context) {
        Intent intent = new Intent(context, KZD_UserVerifyActivity.class);
        context.startActivity(intent);
    }

    private KZD_HttpCallback<KZD_UserInfo> httpListenerVerify = new KZD_HttpCallback<KZD_UserInfo>() {
        @Override
        public void onSuccess(KZD_UserInfo data) {
            toast("认证成功");
            finish();
        }

        @Override
        public void onError(String error) {
            toast(error);
        }
    };

    @Override
    public String getLayoutIdByName() {
        return "kzd_activity_user_verify";
    }

    @Override
    public View getLayoutView() {
        return null;
    }

    private EditText etUserVerifyCardName, etUserVerifyCardCode;
    private TextView tvUserVerifySubmit;

    @Override
    public void initView() {
        etUserVerifyCardName = findViewById(AppUtils.getId((this), "etUserVerifyCardName"));
        etUserVerifyCardCode = findViewById(AppUtils.getId((this), "etUserVerifyCardCode"));
        tvUserVerifySubmit = findViewById(AppUtils.getId((this), "tvUserVerifySubmit"));

        findViewById(AppUtils.getId((this), "ivBackPressed"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                });

        tvUserVerifySubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String cardName = etUserVerifyCardName.getText().toString();
                String cardCode = etUserVerifyCardCode.getText().toString();
                if (TextUtils.isEmpty(cardName)) {
                    toast("请输入姓名");
                } else if (TextUtils.isEmpty(cardCode)) {
                    toast("请输入身份证号");
                } else {
                    KZD_ApiManager.httpVerifyName(cardName, cardCode, httpListenerVerify);
                }
            }
        });
    }

    @Override
    public void initData() {
        KZD_UserInfo userInfo = KZD_SdkController.getUserInfo();
        if (userInfo == null) {
            return;
        }
        if (userInfo.getVerify_info() != 1) {
            return;
        }
        etUserVerifyCardName.setEnabled(false);
        etUserVerifyCardCode.setEnabled(false);
        tvUserVerifySubmit.setEnabled(false);
        etUserVerifyCardName.setText(userInfo.getName());
        etUserVerifyCardCode.setText(userInfo.getIdcard());
        tvUserVerifySubmit.setText("已实名认证");
    }
}
